#!/bin/bash
#=================================================================
# date: 2021-06-25 14:59:28
# title: run_srs
# author: QRS
#=================================================================


CUR_FIL=${BASH_SOURCE[0]}
TOP_DIR=`cd $(dirname $CUR_FIL)/..; pwd`

VENDOR=hzcsai_com
PROJECT=frepai
# VERSION=srs.v4.0.167
VERSION=
if [[ x$VERSION == x ]]
then
    REPOSITORY="$VENDOR/$PROJECT"
else
    REPOSITORY="$VENDOR/$PROJECT:$VERSION"
fi

SS=srs

cmd=""
arg=""

if [[ x$1 == xdev ]]
then
    arg="it --restart unless-stopped"
    shift
else
    cmd="-s $SS"
fi

__find_netip() {
    result=`curl -s icanhazip.com`
    if [[ x$result != x ]]
    then
        echo "$result"
        return
    fi
    result=`wget -qO - ifconfig.co`
    if [[ x$result != x ]]
    then
        echo "$result"
        return
    fi
    result=`curl ipecho.net/plain`
    if [[ x$result != x ]]
    then
        echo "$result"
        return
    fi
    exit -1
}


__start__()
{
    docker container stop ${PROJECT}-$SS 2>/dev/null
    docker container rm ${PROJECT}-$SS 2>/dev/null

    docker run -d${arg} --name ${PROJECT}-$SS --restart always \
        --shm-size=10g --ulimit memlock=-1 --ulimit stack=67108864 \
        --network container:${PROJECT}-app \
        --env SRS_CONF=frep \
        --env CANDIDATE=$(__find_netip) \
        --volume /frepai:/frepai\
        --volume $TOP_DIR/app/conf/frep_rtc.conf:/usr/local/srs/conf/frep.conf \
        --volume $TOP_DIR/entrypoint.sh:/entrypoint.sh \
        $REPOSITORY $cmd
}
__start__
